(function (G) {
    const javaUrl = G.Storage.get('JAVAURL')
    G.vue({
        data: {
            APIURL: G.Storage.get('APIURL'),
            keyWord: '',
            tabList: [],
            cur: 0,
            listArr: [],
            pageCount: 1,
            page: 1,
            tabCur: 0,

            safeKownTabs: [],
            comm_type: '',
            safeKownList: [],
            safeKnowCur: 0,
            safeKnowKeyWord: '',
            safeKnowPage: 1,
            isKnowMore: false,

            safeStudyTabs: [],
            safeStudyList: [],
            theme_type: '',
            safeStudyCur: 0,
            safeStudyKeyWord: '',
            safeStudyPage: 1,
            isStudyMore: true,

            footerTabs: [{
                    selectIcon: '/images/safe/safeKnowSelect.png',
                    icon: '/images/safe/safeKnow.png',
                    text: '安全常识'
                },
                {
                    selectIcon: '/images/safe/studySelect.png',
                    icon: '/images/safe/study.png',
                    text: '安全学习'
                },
            ],
            triggered: false,
            dealType:'pull'

        },
        methods: {
            getInput(e) {
                const {
                    tabCur
                } = this.data
                const value = e.detail.value
                let obj = {
                    keyWord: value
                }
                if (tabCur == 0) {
                    obj.safeKnowKeyWord = value
                } else {
                    obj.safeStudyKeyWord = value
                }
                this.setData(obj)
            },
            tabHeadrChange(e) {
                const {
                    name,
                    index
                } = e.currentTarget.dataset
                const {
                    tabCur
                } = this.data
                console.log(name, 'name')

                if (tabCur == 0) {
                    this.setData({
                        page: 1,
                        safeKnowCur: index,
                        cur: index,
                        comm_type: name == '全部' ? '' : name,
                        listArr: [],
                        safeKownList: []
                    })
                    this.getSafeKnow()
                } else {
                    this.setData({
                        page: 1,
                        safeStudyCur: index,
                        cur: index,
                        theme_type: name == '全部' ? '' : name,
                        listArr: [],
                        safeStudyList: []
                    })
                    this.getSafeStudy()
                }
            },
            footerTabChange(e) {
                const {
                    index
                } = e.currentTarget.dataset
                const {
                    safeKownTabs,
                    safeKnowCur,
                    safeKownList,
                    safeKnowKeyWord,
                    safeKnowPage,

                    safeStudyKeyWord,
                    safeStudyTabs,
                    safeStudyCur,
                    safeStudyPage,
                    safeStudyList
                } = this.data

                const kenowObj = {
                    keyWord: safeKnowKeyWord,
                    page: safeKnowPage,
                    tabList: safeKownTabs,
                    cur: safeKnowCur,
                    listArr: safeKownList

                }
                const studyObj = {
                    keyWord: safeStudyKeyWord,
                    page: safeStudyPage,
                    tabList: safeStudyTabs,
                    cur: safeStudyCur,
                    listArr: safeStudyList
                }
                const typeObj = {
                    0: kenowObj,
                    1: studyObj
                }
                let obj = {
                    tabCur: index,
                    ...typeObj[index]
                }
                this.setData(obj)
                if (index == 0 && safeKownList.length == 0) {
                    this.getSafeKnow()
                } else if (safeStudyList.length == 0) {
                    this.getSafeStudy()
                }

            },
            searchHandle() {
                const {
                    tabCur,
                    keyWord
                } = this.data
              

                if (tabCur == 0) {
                    this.setData({
                        cur: 0,
                        safeKnowCur: 0,
                        comm_type: '',
                        listArr: [],
                        safeKownList: [],
                        safeKnowPage:1,
                        page:1
                    })
                    this.getSafeKnow()
                } else {
                    this.setData({
                        cur: 0,
                        safeStudyCur: 0,
                        theme_type: '',
                        listArr: [],
                        safeStudyList: [],
                        safeStudyPage:1,
                        page:1
                    })
                    this.getSafeStudy()
                }

            },
            // 获取安全常识 tab
            getSafeKownTabs() {
                G.get(javaUrl + '/security/get_sctypes').then(rej => {
                    let list = rej.data ? ['全部', ...rej.data] : []
                    this.setData({
                        safeKownTabs: list,
                        tabList: list
                    })
                    this.getSafeKnow()

                })
            },
            // 获取安全学习 tab
            getSafeStudyTabs() {
                G.get(javaUrl + '/security/get_stypes').then(rej => {
                    let list = rej.data ? ['全部', ...rej.data] : []
                    this.setData({
                        safeStudyTabs: list
                    })


                })
            },
            // 获取安全知识
            getSafeKnow() {
                const {
                    keyWord,
                    comm_type,
                    page,
                    safeKownList

                } = this.data
                let params = {
                    pageNum: page,
                    pageSize: 10,
                    condition: keyWord,
                    comm_type
                }
                G.get(javaUrl + '/security/get_scs', params).then(rej => {
                    let newList = rej.rows && rej.rows.map(item => {
                        return {
                            iconFileId: item.icon_id,
                            title: item.comm_title,
                            time: item.createTimeString,
                            noText: `观看 ${item.visit_nos || 0} 次`,
                            id: item.id,
                            content: item.comm_content,
                            visit_nos:item.visit_nos || 0
                        }
                    })
                    let list = [...safeKownList, ...newList]
                    let isKnowMore = true
                    if (rej.rows && rej.rows.length < 10) isKnowMore = false
                    this.setData({
                        listArr: list,
                        safeKownList: list,
                        isKnowMore,
                        triggered:false
                    })


                })
            },
            // 获取安全学习数据
            getSafeStudy() {
                const {
                    keyWord,
                    theme_type,
                    safeStudyPage,
                    safeStudyList
                } = this.data
                let params = {
                    pageNum: safeStudyPage,
                    pageSize: 10,
                    theme_name: keyWord,
                    theme_type
                }
                G.get(javaUrl + '/security/themes', params).then(rej => {

                    let newList = rej.rows && rej.rows.map(item => {
                        return {
                            iconFileId: item.icon_file_id,
                            title: item.theme_name,
                            time: item.createTimeString,
                            fileId: item.theme_file_id,
                            id: item.id,
                            fileType: item.theme_cate
                        }
                    })
                    let list = [...safeStudyList, ...newList]
                    let isStudyMore = true
                    if (rej.rows && rej.rows.length < 10) isStudyMore = false
                    this.setData({
                        listArr: list,
                        safeStudyList: list,
                        isStudyMore,
                        triggered:false
                    })
                })
            },
            toDetail(e) {
                const {
                    tabCur,
                    APIURL
                } = this.data
                const {
                    item
                } = e.currentTarget.dataset
                let that = this
                console.log(item)
                if (tabCur == 0) {
                    wx.navigateTo({
                        url: '/pages/safetyTraining/safeKnowDetail/index?id=' + item.id 
                    })
                    G.Storage.set('isToDetail',true)
                } else {
                    if (item.fileType == 'video') {
                        wx.navigateTo({
                            url: '/pages/safetyTraining/videoDetail/index?id=' + item.id + '&fileid=' + item.fileId + '&title=' + item.title
                        })
                        return
                    }
                    wx.downloadFile({
                        url: APIURL + '/file/' + item.fileId,
                        success: function (res) {
                            const filePath = res.tempFilePath
                            wx.openDocument({
                                filePath: filePath,
                                fileType: item.fileType,
                                success: function (res) {
                                    console.log('打开文档成功')
                                    that.stduyStatic(item)
                                }
                            })
                        }
                    })
                }

            },
            //安全学习阅读统计
            stduyStatic(info) {
                console.log(info,'infoinfo')
                let data = {
                    theme_id: info.id,
                    learning_times: (Number(info.learning_times) || 0) + 1,
                    createTimeString: G.date(-(-new Date())).Format('yyyy-MM-dd hh:mm:ss'),
                    emp_id:G.Storage.get('userInfo').userId
                }
                G.post(javaUrl + '/security/add_update_Sl', data).then(rej => {

                })
            },
         
            refresherpulling(e) {
                console.log('下拉刷新',e)
                if(e.detail.dy<10) return
              this.resetList('pull')

            },
            resetList (type) {
                const {
                    tabCur
                } = this.data
     
                if (tabCur == 0) {
                    this.setData({
                        triggered: true,
                        listArr: [],
                        page: 1,
                        safeKnowPage: 1,
                        safeKownList: [],
                        dealType:type
                    })
                    this.getSafeKnow()

                } else {
                    this.setData({
                        triggered: true,
                        listArr: [],
                        page: 1,
                        safeStudyList: [],
                        safeStudyPage: 1,
                        dealType:type
                    })
                    this.getSafeStudy()
                }

            },
            scrolltolower() {
                console.log('滚动到底部')
                const {
                    page,
                    tabCur,
                    isKnowMore,
                    isStudyMore,

                } = this.data
                let pageNum = page + 1
                if (tabCur == 0 && isKnowMore == false) {

                    this.setData({
                        page: pageNum,
                        safeKnowPage: pageNum
                    })
                    this.getSafeKnow()
                } else if (tabCur == 1 && isStudyMore == false) {

                    this.setData({
                        page: pageNum,
                        safeStudyPage: pageNum
                    })
                    this.getSafeStudy()
                }


            },
            onShow (){
                console.log('onShow')
                if (G.Storage.get('isToDetail')) {
                    this.resetList('update')
                    G.Storage.set('isToDetail',false)
                }
               
            },
  
        },
        mounted() {
            console.log('mounted')

                this.getSafeKownTabs()
                this.getSafeStudyTabs()
        },
   

    })
})(wx.$)